//
//  WYCustomAnnotationView.m
//  Boss
//
//  Created by admin on 2018/6/1.
//  Copyright © 2018年 admin. All rights reserved.
//

#import "WYCustomAnnotationView.h"

@interface WYCustomAnnotationView()

//@property (nonatomic, strong) UIImageView *portraitImageView;
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic) CGRect rect;
@property (nonatomic,strong)CADisplayLink *displayLink;
@end

@implementation WYCustomAnnotationView
@synthesize calloutView;
@synthesize portraitImageView   = _portraitImageView;
@synthesize nameLabel           = _nameLabel;
#pragma mark - Handle Action

- (void)btnAction
{
    CLLocationCoordinate2D coorinate = [self.annotation coordinate];
    
    NSLog(@"coordinate = {%f, %f}", coorinate.latitude, coorinate.longitude);
}

#pragma mark - Override

- (NSString *)name
{
    return self.nameLabel.text;
}

- (void)setName:(NSString *)name
{
    self.nameLabel.text = name;
}

- (UIImage *)portrait
{
    return self.portraitImageView.image;
}

- (void)setPortrait:(UIImage *)portrait
{
    self.portraitImageView.image = portrait;
    
    CAKeyframeAnimation *shakeAnim = [CAKeyframeAnimation animation];
    
    shakeAnim.keyPath = @"transform.translation.x";
    
    shakeAnim.duration = 0.8;
    
    CGFloat delta = 10;
    
    shakeAnim.values = @[@10 , @(-delta), @(delta), @10];
    
    shakeAnim.repeatCount = MAXFLOAT;
    
    //[self.portraitImageView.layer addAnimation:shakeAnim forKey:nil];
    //[self startDisplayLink];
    
}
- (void)startDisplayLink{
    self.displayLink = [CADisplayLink displayLinkWithTarget:self
                                                   selector:@selector(handleDisplayLink:)];
    [self.displayLink addToRunLoop:[NSRunLoop currentRunLoop]
                           forMode:NSDefaultRunLoopMode];
}

- (void)handleDisplayLink:(CADisplayLink *)displayLink{
    //do something
    NSLog(@"1231312312312312312313123123112213123");
}
- (void)setSelected:(BOOL)selected
{
    [self setSelected:selected animated:NO];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    if (self.selected == selected)
    {
        return;
    }
    
    if (selected)
    {
        if (self.calloutView == nil)
        {
//            /* Construct custom callout. */
//            self.calloutView = [[CustomCalloutView alloc] initWithFrame:CGRectMake(0, 0, kCalloutWidth, kCalloutHeight)];
//            self.calloutView.center = CGPointMake(CGRectGetWidth(self.bounds) / 2.f + self.calloutOffset.x,
//                                                  -CGRectGetHeight(self.calloutView.bounds) / 2.f + self.calloutOffset.y);
//
//            UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//            btn.frame = CGRectMake(10, 10, 40, 40);
//            [btn setTitle:@"Test" forState:UIControlStateNormal];
//            [btn setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
//            [btn setBackgroundColor:[UIColor whiteColor]];
//            [btn addTarget:self action:@selector(btnAction) forControlEvents:UIControlEventTouchUpInside];
//
//            [self.calloutView addSubview:btn];
//
//            UILabel *name = [[UILabel alloc] initWithFrame:CGRectMake(60, 10, 100, 30)];
//            name.backgroundColor = [UIColor clearColor];
//            name.textColor = [UIColor whiteColor];
//            name.text = @"Hello Amap!";
//            [self.calloutView addSubview:name];
        }
        
        [self addSubview:self.calloutView];
    }
    else
    {
        [self.calloutView removeFromSuperview];
    }
    
    [super setSelected:selected animated:animated];
}

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
    BOOL inside = [super pointInside:point withEvent:event];
    /* Points that lie outside the receiver’s bounds are never reported as hits,
     even if they actually lie within one of the receiver’s subviews.
     This can occur if the current view’s clipsToBounds property is set to NO and the affected subview extends beyond the view’s bounds.
     */
    if (!inside && self.selected)
    {
        inside = [self.calloutView pointInside:[self convertPoint:point toView:self.calloutView] withEvent:event];
    }
    
    return inside;
}

#pragma mark - Life Cycle

- (id)initWithAnnotation:(id<MAAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
    
    if (self)
    {
        self.bounds = CGRectMake(0.f, 0.f, 44, 56);
        self.portraitImageView = [[UIImageView alloc] initWithFrame:CGRectMake(11, 14, 36, 44)];
        [self addSubview:self.portraitImageView];
//        CABasicAnimation *momAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
//        momAnimation.fromValue = [NSNumber numberWithFloat:-0.3];
//        momAnimation.toValue = [NSNumber numberWithFloat:0.3];
//        momAnimation.duration = 0.5;
//        momAnimation.repeatCount = CGFLOAT_MAX;
//        momAnimation.autoreverses = YES;
//       // momAnimation.delegate = self;
//        [self.portraitImageView.layer addAnimation:momAnimation forKey:@"animateLayer"];
        
       
        
        
        
        
        
        
        
        
    }
    
    
    
    
    
    
    
    
    
    return self;
}

-(void)start
{
    
        CABasicAnimation* rotationAnimation;

        //绕哪个轴，那么就改成什么：这里是绕y轴 ---> transform.rotation.y

        rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
        //旋转角度
        rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI];
        //每次旋转的时间（单位秒）
        rotationAnimation.duration = 0.5;
        rotationAnimation.cumulative = YES;
        //重复旋转的次数，如果你想要无数次，那么设置成MAXFLOAT
        rotationAnimation.repeatCount = MAXFLOAT;
    
        [self.portraitImageView.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
    
    
    
    
    
}



@end
